import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EdubaseSemesterApi } from '#/api/edu/base/setting/semester';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { getAcademicPeriodList } from '#/api/edu/base/setting/academicPeriod';
import { getAcademicYearPage } from '#/api/edu/base/setting/academicYear';
import DictBadge from '#/components/dict-badge/dict-badge.vue';
import DictTag from '#/components/dict-tag/dict-tag.vue';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'academicYear',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'academicYearId',
      label: '学年',
      component: 'ApiPagingSelect',
      rules: 'required',
      componentProps(values) {
        return {
          allowClear: true,
          async api() {
            return await getAcademicYearPage(
              { status: 1, pageNo: 1, pageSize: 20 },
              false,
              false,
            );
          },
          labelField: 'academicName',
          valueField: 'id',
          placeholder: '请选择学年',
          onChange: (value, option) => {
            values.academicYear = value ? option.academicYear : '';
          },
        };
      },
    },
    {
      fieldName: 'period',
      label: '学段',
      rules: 'required',
      component: 'ApiSelect',
      componentProps: {
        api: getAcademicPeriodList,
        labelField: 'periodName',
        valueField: 'period',
        placeholder: '请选择学段',
        disabled: true,
      },
      dependencies: {
        triggerFields: ['period'],
        show: (values) => values.period !== 0,
      },
    },
    {
      fieldName: 'type',
      label: '学期类型',
      rules: 'required',
      component: 'Select',
      componentProps(values) {
        return {
          options: getDictOptions(DICT_TYPE.EDU_SEMESTER_TYPE),
          placeholder: '请选择学期类型',
          onChange: (value, option) => {
            values.name = value ? values.academicYear + option.label : '';
          },
        };
      },
    },
    {
      fieldName: 'name',
      label: '学期名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入学期名称',
      },
    },
    {
      fieldName: 'startDate',
      label: '开始日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'endDate',
      label: '结束日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'weekCount',
      label: '总周数',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入总周数',
      },
    },
    {
      fieldName: 'courseStartWeek',
      label: '教学周开始',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入教学课程周开始',
      },
    },
    {
      fieldName: 'courseEndWeek',
      label: '教学周结束',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入教学课程周结束',
      },
    },
    {
      fieldName: 'teachingWeeks',
      label: '教学周',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入教学周',
      },
    },
    {
      fieldName: 'examWeeks',
      label: '考试周',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入考试周',
      },
    },
    {
      fieldName: 'holidayWeeks',
      label: '假期周',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入假期周',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'isCurrent',
      label: '当前学期',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          { label: '是', value: true, colorType: 'primary' },
          { label: '否', value: false, colorType: 'danger' },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      rules: 'required',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '学年id',
      field: 'academicYearId',
    },
    {
      label: '学年',
      field: 'academicYear',
    },
    {
      label: '学段',
      field: 'period',
      render: (val) =>
        h(DictTag, {
          options: [
            { label: '默认', value: 0 },
            ...getDictOptions(DICT_TYPE.EDU_PERIOD),
          ],
          value: val,
        }),
    },
    {
      label: '学期类型',
      field: 'type',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.EDU_SEMESTER_TYPE,
          value: val,
        }),
    },
    {
      label: '学期编码',
      field: 'semesterCode',
    },
    {
      label: '学期名称',
      field: 'name',
    },
    {
      label: '开始日期',
      field: 'startDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '结束日期',
      field: 'endDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '总周数',
      field: 'weekCount',
    },
    {
      label: '教学周开始',
      field: 'courseStartWeek',
    },
    {
      label: '教学周结束',
      field: 'courseEndWeek',
    },
    {
      label: '教学周',
      field: 'teachingWeeks',
    },
    {
      label: '考试周',
      field: 'examWeeks',
    },
    {
      label: '假期周',
      field: 'holidayWeeks',
    },
    {
      label: '状态',
      field: 'status',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        }),
    },
    {
      label: '当前学期',
      field: 'isCurrentName',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '学期参数',
      field: 'metadata',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '备注',
      field: 'remark',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'academicYear',
      label: '学年',
      component: 'ApiPagingSelect',
      componentProps: {
        allowClear: true,
        async api() {
          return await getAcademicYearPage(
            { status: 1, pageNo: 1, pageSize: 20 },
            false,
            false,
          );
        },
        labelField: 'academicName',
        valueField: 'id',
        placeholder: '请选择学年',
      },
    },
    {
      fieldName: 'type',
      label: '学期类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_SEMESTER_TYPE),
        placeholder: '请选择学期类型',
      },
    },
    {
      fieldName: 'semesterCode',
      label: '学期编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入学期编码',
      },
    },
    {
      fieldName: 'name',
      label: '学期名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入学期名称',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'isCurrent',
      label: '是否当前学期',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '是', value: true, colorType: 'primary' },
          { label: '否', value: false, colorType: 'danger' },
        ],
        placeholder: '请选择是否当前学期',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EdubaseSemesterApi.Semester>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '学期主键',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'academicYearId',
      title: '学年id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'academicYear',
      title: '学年',
      minWidth: 80,
    },
    {
      field: 'period',
      title: '学段',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '默认', value: 0 },
            ...getDictOptions(DICT_TYPE.EDU_PERIOD),
          ],
        },
      },
    },
    {
      field: 'type',
      title: '学期类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          type: DICT_TYPE.EDU_SEMESTER_TYPE,
        },
      },
    },
    {
      field: 'semesterCode',
      title: '学期编码',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '学期名称',
      minWidth: 120,
    },
    {
      field: 'startDate',
      title: '开始日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'endDate',
      title: '结束日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'weekCount',
      title: '总周数',
      minWidth: 120,
    },
    {
      field: 'courseStartWeek',
      title: '教学课程周开始',
      minWidth: 120,
    },
    {
      field: 'courseEndWeek',
      title: '教学课程周结束',
      minWidth: 120,
    },
    {
      field: 'teachingWeeks',
      title: '教学周',
      minWidth: 120,
    },
    {
      field: 'examWeeks',
      title: '考试周',
      minWidth: 120,
    },
    {
      field: 'holidayWeeks',
      title: '假期周',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'isCurrent',
      title: '是否当前学期',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 200,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
